/**
 * 155. 最小栈
 * https://leetcode-cn.com/problems/min-stack/
 */

function MinStack() {
  this.stack = new Array(10);
  this.length = 0;
  this.min = Infinity;
}

/**
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function push(val) {
  if (this.length === this.stack.length) {
    this.stack.length *= 2;
  }
  this.stack[this.length] = val;
  this.length += 1;
  this.min = Math.min(this.min, val);
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function pop() {
  this.length -= 1;
  let min = Infinity;
  for (let i = 0; i < this.length; i += 1) {
    if (this.stack[i] < min) min = this.stack[i];
  }
  this.min = min;
};

/**
 * @return {number}
 */
MinStack.prototype.top = function top() {
  return this.stack[this.length - 1];
};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function getMin() {
  return this.min;
};

const minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
console.log(minStack.getMin() === -3); // --> 返回 -3
minStack.pop();
console.log(minStack.top() === 0); // --> 返回 0
console.log(minStack.getMin() === -2); // --> 返回 -2
